import 'package:shared_preferences/shared_preferences.dart';

class SPUts {
  //私有化构造函数
  SPUts._();

  static Future<SharedPreferences> getSp() async {
    return await SharedPreferences.getInstance();
  }

  static Future<T> getValue<T>(String key) async {
    SharedPreferences preferences = await getSp();
    T val = preferences.get(key);
    return val;
  }

  static Future<void> setString(String key, String value) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setString(key, value);
  }

  static Future<void> setInt(String key, int value) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setInt(key, value);
  }

  static Future<void> setDouble(String key, double value) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setDouble(key, value);
  }

  static Future<void> setBoolean(String key, bool value) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setBool(key, value);
  }

  static Future<String> getString(String key, String defValues) async {
    String val = await getValue<String>(key);
    return val ?? defValues;
  }

  static Future<int> getInt(String key, int defValues) async {
    int val = await getValue<int>(key);
    return val ?? defValues;
  }

  static Future<double> getDouble(String key, double defValues) async {
    double val = await getValue<double>(key);
    return val ?? defValues;
  }

  static Future<bool> getBoole(String key, bool defValues) async {
    bool val = await getValue<bool>(key);
    return val ?? defValues;
  }

  /// 退出登录 清空所有数据
  static Future<bool> clear() async {
    SharedPreferences sharedPreferences = await getSp();
    return sharedPreferences.clear();
  }
}
